﻿using EventHandlerDemo.Domain.Bases;
using EventHandlerDemo.Domain.OrderEventLogs;
using EventHandlerDemo.Domain.Orders;

namespace EventHandlerDemo.Application.Orders;

public interface IOrderAppService
{
    Task<int> CreateaAsync();
}

public class OrderAppService : IOrderAppService
{
    private readonly IOrderRepository _orderRepository;
    private readonly IOrderEventLogRepository _orderEventLogRepository;
    private readonly IUnitOfWork _unitOfWork;

    public OrderAppService(
        IOrderRepository orderRepository,
        IOrderEventLogRepository orderEventLogRepository,
        IUnitOfWork unitOfWork)
    {
        _orderRepository = orderRepository;
        _orderEventLogRepository = orderEventLogRepository;
        _unitOfWork = unitOfWork;
    }

    public async Task<int> CreateaAsync()
    {
        var order = new Order()
        {
            Name = Guid.NewGuid().ToString(),
        };
        await _orderRepository.InsertAsync(order);

        var orderEventLog = new OrderEventLog()
        {
            OrderId = order.Id,
            Description = "test"
        };
        await _orderEventLogRepository.InsertAsync(orderEventLog);
        var result = await _unitOfWork.CommitAsync();
        return result;
    }
}